package com.servlet;

import com.pojo.Info;
import com.util.BaseServlet;
import com.util.ResultData;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

import static com.util.Vessel.infoSer;

@WebServlet("/info")
public class InfoServlet extends BaseServlet {
    @Override
    public Class getServlet() {
        return InfoServlet.class;
    }

    //查询
    public String queryInfo(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        List<Info> infoList = infoSer.queryInfo();
        session.setAttribute("infoList",infoList);
        return "infoList";
    }

    //新增关键词
    public void addInfo(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        String date = request.getParameter("date");
        Info info = new Info();
        info.setName(name);
        info.setType(type);
        info.setDate(date);
        int num = 0;
        num = infoSer.addInfo(info);
        if (num > 0){
            try{
                response.sendRedirect("/info?action=queryInfo");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    //删除关键词
    public ResultData delInfo(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        Info info = new Info();
        info.setId(id);
        int num = 0;
        num = infoSer.delInfo(info);
        ResultData resultData = new ResultData();
        if (num > 0){
            resultData.setStatus(200);
            resultData.setMsg("Delete complete!");
        }else {
            resultData.setStatus(200);
            resultData.setMsg("Delete fail!");
        }
        return resultData;
    }
}
